package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pojo.Customer;
import pojo.Product;
import util.ConnData;


public class CustomerDao {
	public Customer login(Customer c) throws ClassNotFoundException, SQLException {
		Customer cu =null;
		Connection  connection = ConnData.getConnection();
		String sql="select * from customer where c_name=? and c_pass=?";
		PreparedStatement pst = connection.prepareStatement(sql);
		pst.setString(1, c.getC_name());
		pst.setString(2, c.getC_pass());
		ResultSet r = pst.executeQuery();
		if(r.next()) {
			cu= new Customer();
			cu.setC_name(r.getString("c_name"));
			cu.setC_pass(r.getString("c_pass"));
		}
		return  cu;
	}
//	public int getTotal() {
////		int flag=0;
////		Connection  connection = ConnData.getConnection();
////		String sql="select count(*) as total from product";
////		try {
////			PreparedStatement ps=conn.prepareStatement(sql);
////			ResultSet rs=ps.executeQuery();
////			if(rs.next()) {
////				flag=rs.getInt(1);
////			}
////		} catch (SQLException e) {
////			// TODO Auto-generated catch block
////			e.printStackTrace();
////		}
////		return flag;
//	}
	public List<Product> PageData(int currentPage,int pageSize) throws ClassNotFoundException, SQLException{
		List<Product> List=new ArrayList<Product>();
		Connection  connection = ConnData.getConnection();
		String sql="select * from product";
		try {
			PreparedStatement ps = connection.prepareStatement(sql,
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			ResultSet rs = ps.executeQuery();
			Product p=null;
			rs.absolute((currentPage-1)*pageSize+1);
			for(int i =0;i<pageSize;i++) {//寰幆pageSize娆�
				p = new Product();
				p.setP_type(rs.getString("p_type"));
				p.setP_id(rs.getString("p_id"));
				p.setP_name(rs.getString("p_name"));
				p.setP_price(rs.getFloat("p_price"));
				p.setP_quantity(rs.getInt("p_quantity"));
				p.setP_image(rs.getString("p_image"));
				p.setP_description(rs.getString("p_description"));
				p.setP_time(rs.getString("p_time"));
				List.add(p);
			if(!rs.next()) {
				break;
			};
		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return List;
	}
//	public List<Product> showProductByType(int currentPage,int pageSize,String type){
//		List<Product> list = new ArrayList<Product>();
//		//杩炴帴鏁版嵁搴�
//		Connection conn = DataConn.getConn();
//		//缂栧啓SQL璇彞
//		String sql = "select * from product where p_type=? limit ?,?";
//		Product p;
//		try {
//			PreparedStatement pst = conn.prepareStatement(sql);
//			pst.setString(1, type);
//			pst.setInt(2,(currentPage-1)*pageSize+1);
//			pst.setInt(3,pageSize);  // pst.setInt(2,curretPage*pageSize);
//			ResultSet rs = pst.executeQuery();
//		while(rs.next()) {
//			p = new Product();
//			p.setP_type(rs.getString("p_type"));
//			p.setP_id(rs.getString(2));
//			p.setP_name(rs.getString(3));
//			p.setP_price(rs.getFloat(4));
//			p.setP_quantity(rs.getInt(5));
//			p.setP_image(rs.getString(6));
//			p.setP_description(rs.getString(7));
//			p.setP_time(rs.getString(8));
//			System.out.println(p.getP_id());
//			list.add(p)	;
//		}
//		rs.close();
//		pst.close();
//		conn.close();
//		}catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return list;
//	}

}
